<template>
  <div class="edit-container">
    <van-nav-bar title="修改昵称" left-text="返回" right-text="确定" @click-left="$emit('close')" @click-right="onConfirm" />
    <div class="mian">
      <van-field v-model="loaclName" rows="2" autosize type="textarea" maxlength="7" placeholder="请输入昵称" show-word-limit />
    </div>
  </div>
</template>
<script>
import { updateUserProfile } from '@/api/user.js'
export default {
  props: {
    value: {
      type: String,
      reuqired: true
    }
  },
  data() {
    return {
      loaclName: this.value
    }
  },
  methods: {
    async onConfirm() {
      if (this.loaclName.trim().length === 0) {
        return this.$toast({
          message: '请输入用户昵称'
        })
      }
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true
      })
      try {
        await updateUserProfile({ name: this.loaclName })
        this.$emit('input', this.loaclName)
        this.$emit('close')
        this.$toast.success('保存成功！')
      } catch (err) {
        if (err && err.response && err.response.status === 409) {
          this.$toast.fail('该昵称已存在！')
        }
      }
    }
  }
}
</script>
<style lang="less" scoped>
.edit-container {
  height: 100%;
  width: 100%;
  background-color: #eee;
  .van-nav-bar {
    background-color: #fff;
  }
  /deep/.van-nav-bar__title {
    color: #1989fa;
  }
  .mian {
    padding: 14px;
  }
}
</style>
